package com.yx.pojo;

import com.alibaba.fastjson.annotation.JSONField;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

@Data
@NoArgsConstructor
public class LoginUser implements UserDetails {

    private Emp emp;

    private List<String> permissions;//存储权限信息

    public LoginUser(Emp emp, List<String> permissions) {
        this.emp = emp;
        this.permissions = permissions;
    }

    //获取权限
    @JSONField(serialize = false) //忽略
    private List<SimpleGrantedAuthority> authorities;//权限信息
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        if(authorities != null){
            return authorities;
        }
        //把permissions中字符串类型的权限信息转换成SimpleGrantedAuthority对象存入authorities
        authorities = permissions.stream().map(SimpleGrantedAuthority::new).collect(Collectors.toList());
        return authorities;
    }

    @Override
    public String getPassword() {
        return emp.getPassword();
    }

    @Override
    public String getUsername() {
        return emp.getUsername();
    }

}
